SIM_ROOT ?= $(shell readlink -f "$(CURDIR)/../")

include $(SIM_ROOT)/Makefile.config

DIRECTORIES := ${shell find $(SIM_ROOT)/common -type d -print} \
	$(SIM_ROOT)/include

LIBCARBON_SOURCES = $(foreach dir,$(DIRECTORIES),$(wildcard $(dir)/*.cc)) \
	$(wildcard $(SIM_ROOT)/common/config/*.cpp)

LIBCARBON_OBJECTS = $(patsubst %.cpp,%.o,$(patsubst %.c,%.o,$(patsubst %.cc,%.o,$(LIBCARBON_SOURCES) ) ) )

INCLUDE_DIRECTORIES = $(DIRECTORIES) $(PIN_HOME)/extras/xed-$(SNIPER_TARGET_ARCH)/include $(PIN_HOME)/extras/xed2-$(SNIPER_TARGET_ARCH)/include $(SIM_ROOT)/linux $(SIM_ROOT)/sift

CLEAN=$(findstring clean,$(MAKECMDGOALS))

ifeq ($(SNIPER_TARGET_ARCH),ia32)
  # Add -march=i686 to enable some extra instructions that allow for implementation of 64-bit atomic adds
  CXXFLAGS += -m32 -march=i686 -DTARGET_IA32
  LD_FLAGS += -m32
endif
ifeq ($(SNIPER_TARGET_ARCH),intel64)
  CXXFLAGS += -fPIC -DTARGET_INTEL64
  LD_FLAGS +=
endif

CXXFLAGS += -DPIN_REV=$(shell $(SIM_ROOT)/tools/pinversion.py $(PIN_HOME) | cut -d. -f3)

include $(SIM_ROOT)/Makefile.config

ifneq ($(BOOST_INCLUDE),)
	CPPFLAGS += -I$(BOOST_INCLUDE)
endif

LD_LIBS += -lsift -lxed -L$(SIM_ROOT)/python_kit/$(SNIPER_TARGET_ARCH)/lib -lpython2.7 -lrt -lz -lsqlite3

LD_FLAGS += -L$(SIM_ROOT)/lib -L$(SIM_ROOT)/sift -L$(PIN_HOME)/extras/xed-$(SNIPER_TARGET_ARCH)/lib

ifneq ($(SQLITE_PATH),)
	CPPFLAGS += -I$(SQLITE_PATH)/include
	LD_FLAGS += -L$(SQLITE_PATH)/lib
endif

export LD_LIBRARY_PATH := $(SIM_ROOT)/lib:$(LD_LIBRARY_PATH)

# Include paths
CPPFLAGS+=$(foreach dir,$(INCLUDE_DIRECTORIES),-I$(dir)) \
          -I$(SIM_ROOT)/python_kit/$(SNIPER_TARGET_ARCH)/include/python2.7

CXXFLAGS+=-c \
          -Wall -Wextra -Wcast-align -Wno-unused-parameter -Wno-unknown-pragmas -std=c++0x -fno-strict-aliasing $(OPT_CFLAGS) #-Werror

%.h %.hh %.hpp:
	@echo >&2
	@echo "        Unable to find $@" >&2
	@echo "        You may want to try running 'make empty_deps'" >&2
	@echo >&2
	@false

# Build rules for dependency generation
%.d: %.cpp
	$(_MSG) '[DEP   ]' $(subst $(shell readlink -f $(SIM_ROOT))/,,$(shell readlink -f $@))
	$(_CMD) $(CXX) -MM -MG $(CPPFLAGS) $(CXXFLAGS) $< | sed -n "H;$$ {g;s@.*:\(.*\)@$*.o $@: \$$\(wildcard\1\)@;p}" >$@

%.d: %.cc
	$(_MSG) '[DEP   ]' $(subst $(shell readlink -f $(SIM_ROOT))/,,$(shell readlink -f $@))
	$(_CMD) $(CXX) -MM -MG $(CPPFLAGS) $(CXXFLAGS) $< | sed -n "H;$$ {g;s@.*:\(.*\)@$*.o $@: \$$\(wildcard\1\)@;p}" >$@

%.d: %.c
	$(_MSG) '[DEP   ]' $(subst $(shell readlink -f $(SIM_ROOT))/,,$(shell readlink -f $@))
	$(_CMD) $(CC) -MM -MG $(CPPFLAGS) $(CFLAGS) $< | sed -n "H;$$ {g;s@.*:\(.*\)@$*.o $@: \$$\(wildcard\1\)@;p}" >$@

# Build rules for object generation
%.o: %.cpp
	$(_MSG) '[CXX   ]' $(subst $(shell readlink -f $(SIM_ROOT))/,,$(shell readlink -f $@))
	$(_CMD) $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@

%.o : %.cc
	$(_MSG) '[CXX   ]' $(subst $(shell readlink -f $(SIM_ROOT))/,,$(shell readlink -f $@))
	$(_CMD) $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@

%.o : %.c
	$(_MSG) '[CC    ]' $(subst $(shell readlink -f $(SIM_ROOT))/,,$(shell readlink -f $@))
	$(_CMD) $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
